package com.example.zbh.lessona.file;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

import com.example.zbh.lessona.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by dllo on 15/12/17.
 */
public class FileActivity extends Activity {
    String string = "南朝东";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        File file = Environment.getExternalStorageDirectory();
        String filePath = file.getAbsolutePath() + "/nanchaodong";
        File file1 = new File(filePath);
        if (!file1.exists()) {
            file1.mkdir();
            Log.i("nanchaodong", " >>>>>>>>>>");
        }
        File file2 = new File(file1, "gedabao.txt");
        if (!file2.exists()) {
            try {
                file2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file2);
            byte[] bytes = string.getBytes();
            fileOutputStream.write(bytes);
            fileOutputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            FileInputStream fileInputStream = new FileInputStream(file2);
            int l = fileInputStream.available();
            byte[] bytes = new byte[l];
            fileInputStream.read(bytes);
            String s = new String(bytes);
            Log.d("ddddddddddddddd", s);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        try {
//            InputStreamReader reader = new InputStreamReader(new FileInputStream(file2));
//            BufferedReader bufferedReader = new BufferedReader(reader);
//            String line = null;
//            while((line = bufferedReader.readLine()) != null){
//            line  += line;
//            }
//
//            Log.d("dddddddd", line);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }
}
